/*
 * @desc:
 * @LastEditTime: 2022-10-31 10:38:14
 * @params:
 * @return::
 */
// import { useErrorMsgStore } from '../store'
// import { tokenConfirmFn } from './modules/user'
import { checkStatus } from './helpers/checkStatus'

const $http = (options) => {
  // const baseUrl = 'http://124.221.82.55:3000/api'
  const baseUrl = 'http://127.0.0.1:3000/api'
  const promise = new Promise((resolve, reject) => {
    uni.request({
      url: baseUrl + options.url,
      method: options.method || 'GET',
      data: options.data,
      timeout: 5000,
      header: {
        'X-LC-Id': '7yoqQxyLBNN9MW5rsgTGXbHL-gzGzoHsz',
        'X-LC_Key': 'hA8yGYuHjnXWRuilrzTe0C3P',
        'Content-Type': 'application/json',
        authorization: uni.getStorageSync('token'),
      },
      success: (res) => {
        const statusCode = res.statusCode
        if (statusCode == 200 || statusCode == 201) {
          // console.log(res)
          resolve(res)
        } else {
          // console.log(res)
          checkStatus(res.statusCode)
          // console.log(11)
          reject(res)
        }
      },
      fail: (err) => {
        console.log(err)
        reject(err)
      },
    })

    uni.addInterceptor('request', {
      invoke(args) {
        // console.log(args)
        uni.showLoading({
          title: '加载中...',
        })
      },
      success(args) {
        // 请求成功后，修改code值为1
        // args.data.code = 1
      },
      fail(err) {
        // console.log('interceptor-fail',err)

        uni.hideLoading()
      },
      complete(res) {
        // console.log('interceptor-complete',res)
        uni.hideLoading()
      },
    })
  })

  return promise
}

export default $http
